package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.CoursetypeMapper;
import com.example.demo.sys.entity.Coursetype;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/sys/coursetype")
public class CoursetypeController extends BaseController {
    @Autowired
    private CoursetypeMapper coursetypeMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
            Integer pageNum,
            Integer pageSize) {
        Result result = new Result();
        Page<Coursetype> page = new Page<Coursetype>(pageNum, pageSize);
        Coursetype parms = new Coursetype();
        QueryWrapper<Coursetype> warpper = new QueryWrapper<Coursetype>(parms);

        result.setData(coursetypeMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/TYpage")
    public Result TYpage() {
        Result result = new Result();
        Page<Coursetype> page = new Page<Coursetype>();
        Coursetype parms = new Coursetype();
        QueryWrapper<Coursetype> warpper = new QueryWrapper<Coursetype>(parms);

        result.setData(coursetypeMapper.selectPage(page, warpper));
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/typesave")//保存课程类型
    public Result save(@RequestBody Coursetype entity) {
        Result result = new Result();
        Coursetype type = new Coursetype();
        type.setCourseType(entity.getCourseType());
        Coursetype typeExit = coursetypeMapper.selectOne(new QueryWrapper<Coursetype>(type));
        if (typeExit == null) {
            coursetypeMapper.insert(entity);
            result.success("操作成功");
        }
        else  result.fail("此课程类型已存在");
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/select")
    public Result select() {
        Result result = new Result();
        Coursetype type = new Coursetype();
        QueryWrapper< Coursetype> Bwarpper = new QueryWrapper< Coursetype>(type);
        //list的课程类型对象
        List<String> courseTypelist = null;;
        courseTypelist = coursetypeMapper.Select();
        Map<String, Object> map = new HashMap<>();
        map.put("courseTypelist", courseTypelist);
        result.setData(map);
        return result;
    }
}
